//
//  XMTypeMaterialListView.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/27.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMTypeMaterialListView.h"
#import "XMTypeListModel.h"

@interface XMTypeMaterialListView ()<UITableViewDelegate,UITableViewDataSource>
{
    typeMaterialListResultBlock _resultBlock;
}
//背景view
@property(nonatomic,strong) UIControl *coverControl;
//内容view
@property (nonatomic,strong) UIView * contentView;
//取消
@property (nonatomic,strong) NSArray * data;
//列表
@property (nonatomic,strong) UITableView * tableView;
//确定
@property (nonatomic,strong) UIButton * sureBtn;
//取消
@property (nonatomic,strong) UIButton * cancelBtn;
@end

@implementation XMTypeMaterialListView

//选择展示回调
+ (void)showTypeMaterialListWithDataArr:(NSArray *)DataArr resultBlock:(typeMaterialListResultBlock)resultBlock{
    
    XMTypeMaterialListView * listView = [[XMTypeMaterialListView alloc]initWithData:DataArr resultBlock:resultBlock];
    [listView show];
    
}

-(instancetype)initWithData:(NSArray *)data resultBlock:(typeMaterialListResultBlock)resultBlock{
    
    if (self = [super init]) {
        self.data = data;
        _resultBlock = resultBlock;
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    self.backgroundColor = kClearColor;
    
    //背景control
    [self.coverControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    //内容
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@300);
        make.height.equalTo(@(200));
    }];
}


#pragma mark 动画

- (void)show{
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(keyWindow);
    }];
    
    self.contentView.alpha = 0;
    self.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        weakSelf.contentView.alpha = 1;
    }];
}

- (void)hide
{
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.alpha = 0;
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {
        [self.coverControl removeAllSubviews];
        [self.coverControl removeFromSuperview];
        [self.contentView removeAllSubviews];
        [self.contentView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

#pragma mark ----action----



//确定
-(void)sureAction{
    if (_resultBlock) {
        _resultBlock(self.data);
    }
    [self hide];
}
//取消
-(void)cancelAction{

    [self hide];
}

#pragma mark ----tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XMTypeMaterialListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[XMTypeMaterialListTableViewCell className]];
    if (!cell) {
        cell = [[XMTypeMaterialListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[XMTypeMaterialListTableViewCell className]];
    }
    XMTypeListClassifyModel * model = self.data[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XMTypeListClassifyModel * model = self.data[indexPath.row];
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];
}

#pragma mark ----getting&setting----

//背景颜色control
-(UIControl *)coverControl{
    if (!_coverControl) {
        _coverControl = [[UIControl alloc]init];
        _coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self addSubview:_coverControl];
        
    }
    return _coverControl;
}

//内容
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = kWhiteColor;
        _contentView.layer.cornerRadius = 4.0f;
        _contentView.clipsToBounds = YES;
        //标题
        UILabel * titleLabel  = [[UILabel alloc]init];
        titleLabel.text = @"材料:";
        titleLabel.textColor = kTextBlackColor;
        titleLabel.font = kFont(14);
        [_contentView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.centerX.equalTo(_contentView);
            make.height.equalTo(@45);
            
        }];
    

        //确定
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(_contentView);
            make.left.equalTo(_contentView.mas_centerX);
            make.height.equalTo(@45);
        }];
        
        //取消
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.equalTo(_contentView);
            make.right.equalTo(_contentView.mas_centerX);
            make.height.equalTo(@45);
        }];
        
        //列表
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom);
            make.bottom.equalTo(self.sureBtn.mas_top);
            make.left.right.equalTo(_contentView);
        }];
        
        
        [self addSubview:_contentView];
    }
    return _contentView;
    
}

//确定
-(UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = kFont(14);
        [_sureBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
        [_sureBtn setBackgroundColor:kBgColor];
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_sureBtn];
    }
    return _sureBtn;
}

//取消
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kFont(14);
        [_cancelBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
        [_cancelBtn setBackgroundColor:kBgColor];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}

@end

@interface XMTypeMaterialListTableViewCell ()

//图标
@property (nonatomic,strong) UILabel * titleLabel;

//选择图片
@property (nonatomic,strong) UIButton * selectBtn;
@end

@implementation XMTypeMaterialListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
        
    }
    return self;
}

-(void)setUI{
    
    //标题
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.centerY.equalTo(self.contentView);
    }];
    
    //选择图标
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.contentView);
        make.width.height.equalTo(@27);
    }];
    
    
}

#pragma mark ----getting&setting----


//标题
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = kTextBlackColor;
        _titleLabel.font = kFont(12);
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

//选择图标
-(UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setImage:kImage(@"ic_album_unchoose_border") forState:UIControlStateNormal];
        [_selectBtn setImage:kImage(@"ic_album_choosed") forState:UIControlStateSelected];
        _selectBtn.userInteractionEnabled = NO;
        [self.contentView addSubview:_selectBtn];
    }
    return _selectBtn;
}


-(void)setModel:(XMTypeListClassifyModel *)model{
    _model = model;
    self.titleLabel.text = model.name;
    self.selectBtn.selected = model.isSelected;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
